{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Arrays\n",
    "\n",
    "Arrays are data structures that can contain multiple values, similar to lists in Python, but they only contain one type of data i.e. all integers or all characters, and they are often used to represent a robot's environment. This is easiest to see in an example. Say we have a self-driving car driving on a one-lane road, and near the end of this road is a stop sign, as in the below image.\n",
    "\n",
    "<img src=\"files/images/road1.png\" width=\"50%\" height=\"50%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### Road Array\n",
    "\n",
    "We can treat this road as an array, and break it up into grid cells for a robot to understand; each grid cell will contain information about the road that we can use to help our car navigate!\n",
    "\n",
    "<img src=\"files/images/numbered_grid2.png\" width=\"50%\" height=\"50%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In Python code, the road will contain two character values: `'r'` for road and `'s'` for stop sign. Then we can create a program that iterates through this array, checks for the stop sign (the 's') and makes the car stop at the cell *right in front of* (in this case, one cell before) the stop sign.\n",
    "\n",
    "Note that in this demonstration, we'll be using the `numpy` library, which is a Python library that helps us create and manipulate arrays; it's commonly used in a variety of applications including self-driving cars, and we'll see it come up again and again in code.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# A one-lane road, represented by an array\n",
    "# Here is a 1x7 road\n",
    "road = np.array(['r', 'r', 'r', 'r', 'r', 's', 'r'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Print out some information about the array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The length of this array is: 7\n"
     ]
    }
   ],
   "source": [
    "# Print out some information about this road\n",
    "print('The length of this array is: ' + str(len(road)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read the values in an array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "Value at index [0] = r\n",
      "\n",
      "\n",
      "Value at index [-1] = r\n",
      "\n",
      "\n",
      "Are the first and last values equal? True\n"
     ]
    }
   ],
   "source": [
    "# Access the first index and read its value\n",
    "value = road[0]\n",
    "print('\\n')\n",
    "print('Value at index [0] = ' +str(value))\n",
    "\n",
    "# Read the last item in the array\n",
    "# A negative index moves from the end of the list backwards!\n",
    "value_end = road[-1]\n",
    "print('\\n')\n",
    "print('Value at index [-1] = ' +str(value_end))\n",
    "\n",
    "# Compare first and last values\n",
    "equal = (value == value_end)\n",
    "print('\\n')\n",
    "print('Are the first and last values equal? ' +str(equal))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Array Iteration\n",
    "\n",
    "Iterating, or looping, through an array is a useful way of reading all the information it contains sequentially. The following code demonstrates how to iterate through an entire array and how to iterate until you find a certain location."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "road[0] = r\n",
      "road[1] = r\n",
      "road[2] = r\n",
      "road[3] = r\n",
      "road[4] = r\n",
      "road[5] = s\n",
      "road[6] = r\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# A 1x7 road\n",
    "road = np.array(['r', 'r', 'r', 'r', 'r', 's', 'r'])\n",
    "\n",
    "# Iterate through the array\n",
    "length = len(road)\n",
    "for index in range(0, length):\n",
    "    # Find and store the value at each index\n",
    "    value = road[index]\n",
    "    # Print a new line and the value\n",
    "    print('road['+str(index)+'] = '+str(value))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "We've reached the middle of the road and we're leaving the loop!\n"
     ]
    }
   ],
   "source": [
    "# Iterate and exit the loop (return) once you reach index 3 - the middle\n",
    "for index in range(0, length):\n",
    "    # Check if index is equal to 3\n",
    "    print(str(index))\n",
    "    if index == 3:\n",
    "        print('We\\'ve reached the middle of the road and we\\'re leaving the loop!')\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
